package com.mwy.springquartzdemo.job;

import org.quartz.*;
import org.springframework.stereotype.Component;

import java.util.Date;

//任务类（任务调度器管理的类，调用任务中的代码，执行其操作）

public class MyJob1 implements Job {

    public MyJob1() {
        System.out.println("创建MyJob1实例");
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //JobExcutionContext可以访问Quartz的运行环境和job的明细数据（比如JobDetail和Trigger的内容还有调度器的信息）
        //通过jobExtionContext获取JobDetail
        JobDetail jobDetail = jobExecutionContext.getJobDetail();
        //通过JobDetail获取jobDataMap(其中包含了传递的参数)
        JobDataMap jobDataMap = jobDetail.getJobDataMap();
        String message = (String)jobDataMap.get("message");
        System.out.println(message);
        JobKey key = jobDetail.getKey();
        String name = key.getName();
        String group = key.getGroup();
        //通过jobExcutionContext获取Trigger
        Trigger trigger = jobExecutionContext.getTrigger();
        TriggerKey triggerKey = trigger.getKey();
        String triggerKeyName = triggerKey.getName();
        String triggerKeyGroup = triggerKey.getGroup();
        System.out.println("触发器名称"+triggerKeyName);
        System.out.println("触发器组名："+triggerKeyGroup);
        System.out.println("当前任务名称："+name);
        System.out.println("当前任务组名："+group);
        System.out.println("你好，世界，"+new Date());
    }

    public void run(){
        System.out.println("你好，世界，"+new Date());
    }
}
